﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Ink;


namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 手写功能
    /// 2019-2-20
    /// </summary>
    public partial class frmWrite : Form
    {
        public frmWrite()
        {
            InitializeComponent();
        }

        InkCollector ic;
        RecognizerContext rct;
        string FullCACText;
        
        private void frmWrite_Load(object sender, EventArgs e)
        {
            ic = new InkCollector(this.pictureBox1.Handle);
            this.ic.Stroke += new InkCollectorStrokeEventHandler(ic_Stroke);

            ic.Enabled = true;
            ink_();

            this.rct.RecognitionWithAlternates += new RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternates);

            rct.Strokes = ic.Ink.Strokes;

        }

        void rct_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs e)
        {

            string ResultString = "";
            RecognitionAlternates alts;

            if (e.RecognitionStatus == RecognitionStatus.NoError)
            {
                alts = e.Result.GetAlternatesFromSelection();
                Console.WriteLine(alts.Count);
                foreach (RecognitionAlternate alt in alts)
                {
                    ResultString = ResultString + alt.ToString() + " ";
                }
            }
            FullCACText = ResultString.Trim();
            //Console.WriteLine(FullCACText);
           // MessageBox.Show(FullCACText);
            Control.CheckForIllegalCrossThreadCalls = false;
            GetString(FullCACText);
            Control.CheckForIllegalCrossThreadCalls = true;

        }

        private void GetString(string txt)
        {
            string[] str_temp = txt.Split(' ');
            string str_temp1 = "shibie_";
            string str_temp2 = "";
            if (str_temp.Length > 0)
            {
                for (int i = 0; i < str_temp.Length; i++)
                {
                    str_temp2 = str_temp1 + i.ToString();
                    Control[] con_temp = panel1.Controls.Find(str_temp2, true);
                    if (con_temp.Length > 0)
                    {
                        Button btn = (Button)(con_temp[0]);
                        btn.Text = str_temp[i];
                        btn.Click += new System.EventHandler(this.btn_Click);
                    }
                }
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
           // MessageBox.Show(btn.Text);
            string content = this.textBox1.Text;

            if (content.Length >= 2)
            {
                string before = content.Substring(0, content.Length - 1);
                string temp = content.Substring(content.Length - 1);
                temp = btn.Text;
                this.textBox1.Text = before + temp;
            }
            else
            {
                this.textBox1.Text = btn.Text;
            }

           
        }

        void ic_Stroke(object sender, InkCollectorStrokeEventArgs e)
        {
            rct.StopBackgroundRecognition();
            rct.Strokes.Add(e.Stroke);
            rct.BackgroundRecognizeWithAlternates(0);
        }

        private void ink_()
        {
            Recognizers recos = new Recognizers();
            Recognizer chineseReco = recos.GetDefaultRecognizer();

            rct = chineseReco.CreateRecognizerContext();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //#region 单字识别处理
            //try
            //{
            //    List<Point> lstPoints = new List<Point>();

            //    foreach (var stroke in ic.Ink.Strokes)
            //    {
            //        lstPoints.AddRange(stroke.GetPoints());
            //    }
            //    ic.Ink.DeleteStrokes();//清除手写区域笔画;
            //    Stroke sk = ic.Ink.CreateStroke(lstPoints.ToArray());

            //    rct.Strokes.Add(sk);

            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
            //#endregion

            textBox1.SelectedText = ic.Ink.Strokes.ToString();
        }

        //
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            this.timer1.Enabled = true;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.timer1.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            colorDialog1.FullOpen = true;
            colorDialog1.ShowDialog();
            ic.DefaultDrawingAttributes.Color = colorDialog1.Color;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1.PerformClick();
            if (!ic.CollectingInk)
            {
                Strokes strokesToDelete = ic.Ink.Strokes;
                rct.StopBackgroundRecognition();
                ic.Ink.DeleteStrokes(strokesToDelete);
                rct.Strokes = ic.Ink.Strokes;
                ic.Ink.DeleteStrokes();//清除手写区域笔画;
                this.pictureBox1.Refresh();//刷新手写区域
            }
            this.timer1.Stop();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
        }

        private void shibie_Click(object sender, EventArgs e)
        {
            int intStart = textBox1.SelectionStart;
            textBox1.Text = textBox1.Text.Remove(intStart - 1, 1);
            textBox1.Text = textBox1.Text.Insert(intStart - 1, (sender as Button).Text);
            textBox1.SelectionStart = intStart;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!ic.CollectingInk)
            {
                Strokes strokesToDelete = ic.Ink.Strokes;
                rct.StopBackgroundRecognition();
                ic.Ink.DeleteStrokes(strokesToDelete);
                rct.Strokes = ic.Ink.Strokes;
                ic.Ink.DeleteStrokes();//清除手写区域笔画;
                this.pictureBox1.Refresh();//刷新手写区域
                textBox1.Clear();
            }
        }

       
    }
}
